/*
 * test_c1_program.c
 *
 *  Created on: Nov 24, 2013
 *      Author: adijoshi
 */

#include <stdio.h>

typedef struct CacheLineStruct{
	int Tag,LRU_Status,MESI_Status;
} CacheLine;

CacheLine L2Cache[1][8];

void update_LRU(int set, int way)
{


	int i=0;
	for (i = 0; i<8; i++)
	{
		if ((L2Cache[set][i].LRU_Status!= -1) && (L2Cache[set][i].LRU_Status <= L2Cache[set][way].LRU_Status))
					L2Cache[set][i].LRU_Status = L2Cache[set][i].LRU_Status+1;
	}
	L2Cache[set][way].LRU_Status =0;
}

int find_LRU_way(int set)
{
	int way;
	for (way = 0; way < 8; way++)
	{
		if (L2Cache[set][way].LRU_Status == 7)
		{
			break;
		}
	}
	return way;
}

int main()
{
	int i,j,k;
	int n=101;
	for(i=0;i<1;i++)
		for(j=0;j<8;j++)
		{
			L2Cache[i][j].Tag=n;
			L2Cache[i][j].LRU_Status =7-j;
			n++;
		}

	for(i=0;i<1;i++)
		for(j=0;j<8;j++)
			printf("%d \t %d\t\t",L2Cache[i][j].Tag,L2Cache[i][j].LRU_Status);

	printf("\n");

	int arr[]={102,105,111,101,99};
	for(i=0;i<5;i++)
		printf("%d \t",arr[i]);
	printf("\n");

	char flag;
	for(i=0;i<5;i++)
	{
		flag=' ';
		for(k=0;k<1;k++)
		{
			for(j=0;j<8;j++)
			{
				if(arr[i] == L2Cache[k][j].Tag)
				{
					update_LRU(k,j);
					flag ='F';
				}
			}
		}

		int way=-1;
		if(flag!='F')
		{
			way=find_LRU_way(0);
			printf("Evict %d\n",way);
			//printf("Evict this - %d\n",L2Cache[0][way].Tag);
			//L2Cache[0][way].Tag = arr[i];
		}

	}

	for(i=0;i<1;i++)
		for(j=0;j<8;j++)
			printf("%d \t %d\t\t",L2Cache[i][j].Tag,L2Cache[i][j].LRU_Status);
	return 0;
}
